package com.example.mybatisplusj2300107.controller;

import com.example.mybatisplusj2300107.entity.Student;
import com.example.mybatisplusj2300107.mapper.StudentMapper;
import com.example.mybatisplusj2300107.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/selectAll", produces = "application/json")
    public List<Student> selectAll() {
        return studentService.selectAll();
    }

    @GetMapping(value = "/selectById/{student_id}", produces = "application/json")
    public Student selectById(@PathVariable("student_id") String studentId) {
        return studentService.selectById(studentId);
    }

    @PostMapping(value = "/update", produces = "application/json")
    public Map<String, Object> update(@RequestBody Student student) {
        Map<String, Object> result = new HashMap<>();
        try{
            studentService.update(student);
            result.put("success", true);
            result.put("message", "学生信息更新成功");
        }catch (Exception e){
            result.put("success", false);
            result.put("message", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping(value = "/insert", produces = "application/json")
    public Map<String, Object> insert(@RequestBody Student student) {
        Map<String, Object> result = new HashMap<>();
        try{
            studentService.insert(student);
            result.put("success", true);
            result.put("message", "学生信息添加成功");
        }catch (Exception e){
            result.put("success", false);
            result.put("message", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping(value = "/delete/{student_id}", produces = "application/json")
    public Map<String, Object> delete(@PathVariable("student_id") String studentId) {
        Map<String, Object> result = new HashMap<>();
        try{
            studentService.deleteById(studentId);
            result.put("success", true);
            result.put("message", "学生信息删除成功");
        }catch (Exception e){
            result.put("success", false);
            result.put("message", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
